layui.use(['form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
    
	/*初始化表格*/
	function tableInit(where = {})
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/admin/goods/stockafter.html'
		,where:where
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
			//封装返回数据
			console.log(res);
			
			for (i in res.data) 
            {
				res.data[i]['reason'] = "<a title='确认退货' sid='"+res.data[i]['stock_id']+"' href='javascript:;' class='hfstock'><i class='layui-icon'>&#xe642;</i></a>";
                res.data[i]['reason'] += "<a title='返回门店' sid='"+res.data[i]['stock_id']+"' href='javascript:;' class='fhzb'><i class='layui-icon'>&#xe698;</i></a>";
			}
			return {
			  "code": 10001, //解析接口状态
			  "msg": '', //解析提示文本
			  "count": res.total, //解析数据长度
			  "data": res.data //解析数据列表
			};
		}
		,cols: [[
		  {type: 'checkbox', fixed: 'left'}
		  ,{field:'stock_id', title: '序号'}
		  ,{field:'state', title: '状态'} 
		  ,{field:'goods_name', title: '产品名称'}
		  ,{field:'imei', title: 'IMEI'}
		  ,{field:'sn', title: 'S/N'}
		  ,{field:'period_time', title: '保质期'}
		  ,{field:'shop_id', title: '门店'}
		  ,{field:'position', title: '所在位置'}
		  ,{field:'selling_price', title: '零售单价（元）'}
		  ,{field:'trade_price', title: '批发价（元）'}
		  ,{field:'buying_price', title: '进货单价（元）'}
		  ,{field:'supplier_id', title: '供应商'}
		  ,{field:'contract_sn', title: '合同编号'}
		  ,{field:'reason', title: '操作'}
		]]
		,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		}
		,response: {
			statusCode: 10001
		}
	  });
	}
	tableInit();
    //监听提交
	form.on('submit(sreach)',
	function(data) {
        console.log(data.field);
		tableInit(data.field);
		return false;
	});
    $('body').on('click', '#hfCheckData', function(){
		parent.layer.confirm('你确定确认退货吗?', function(){
            //获取选中数据
          var checkStatus = table.checkStatus('dataTables-example')
          ,data = checkStatus.data;
          console.log(data);
          if(data.length == 0)
          {
              parent.layer.msg('未选择数据', {icon: 5});
              return false;
          }
          var ids = '';
          for (i in data) 
          {
            ids += data[i].stock_id + ',';
          }
          ids=ids.substring(0,ids.length-1);
          StockAjax({ids:ids});
      });
	});
    //恢复库存
    $('body').on('click', '.hfstock', function(){
        var get_this = $(this);
        parent.layer.confirm('你确定确认退货吗?', function(){
            var params = {};
            params.ids = get_this.attr('sid');
            StockAjax(params);
        });
    });
    function StockAjax(params)
    {
       $.ajax({
            type: "POST",
            url: "/admin/goods/recovery.html",
            cache: false, //禁用缓存
            data: params, //传入组装的参数
            dataType: "json",
            success: function (result) {
                if(result.code==10001){
                    tableInit();
                    parent.layer.msg('操作成功', {icon: 1});
                }else{
                    parent.layer.msg(result.msg, {icon: 5});
                }
            },
            error:function () {
                parent.layer.msg('网络错误', {icon: 5});
            }
        }) 
    }
    $('body').on('click', '#fhCheckData', function(){
        parent.layer.confirm('你确定要返回门店吗?', function(){
		//获取选中数据
          var checkStatus = table.checkStatus('dataTables-example')
          ,data = checkStatus.data;
          console.log(data);
          if(data.length == 0)
          {
              parent.layer.msg('未选择数据', {icon: 5});
              return false;
          }
          var ids = '';
          for (i in data) 
          {
            ids += data[i].stock_id + ',';
          }
          ids=ids.substring(0,ids.length-1);
          HeadquartersAjax({ids:ids});
      });
	});
    //返回总部
    $('body').on('click', '.fhzb', function(){
        var get_this = $(this);
        parent.layer.confirm('你确定要返回门店吗?', function(){
            var params = {};
            params.ids = get_this.attr('sid');
            HeadquartersAjax(params);
        });
    });
    function HeadquartersAjax(params)
    {
       $.ajax({
            type: "POST",
            url: "/admin/goods/returnstock.html",
            cache: false, //禁用缓存
            data: params, //传入组装的参数
            dataType: "json",
            success: function (result) {
                if(result.code==10001){
                    tableInit();
                    parent.layer.msg('操作成功', {icon: 1});
                }else{
                    parent.layer.msg(result.msg, {icon: 5});
                }
            },
            error:function () {
                parent.layer.msg('网络错误', {icon: 5});
            }
        }) 
    }
});

